package com.qfedu.aunt.commons.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qfedu.aunt.commons.exception.BusinessException;
import com.qfedu.aunt.commons.pojo.Orders;
import com.qfedu.aunt.commons.mapper.OrdersMapper;
import com.qfedu.aunt.commons.result.ResponseCode;
import com.qfedu.aunt.commons.service.OrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author xiaobobo
 * @since 2022-10-26
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public IPage<Orders> findOrdersByCityNameAndType(String cityName, String orderType, Integer currentPage, Integer pageSize) throws Exception {
        if(StringUtils.isEmpty(cityName)||StringUtils.isEmpty(orderType)||
           null==currentPage||null==pageSize){
            throw new BusinessException(ResponseCode.PARAMETER_ERROR.getCode(),
                    ResponseCode.PARAMETER_ERROR.getMessage());
        }
        //程序执行到这里 说明没有问题
        //接下来就应该执行查询

        IPage<Orders> page=new Page<>();
        page.setCurrent(currentPage);
        page.setPages(pageSize);

        //构建我们的查询条件
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.eq("city_name",cityName);
        ordersQueryWrapper.eq("orders_type",orderType);

        //接下来进行查询
        IPage<Orders> ordersIPage = ordersMapper.selectPage(page, ordersQueryWrapper);
        return ordersIPage;
    }

    @Override
    public IPage<Orders> findOrdersByCityName(String cityName, Integer currentPage, Integer pageSize) {
        if(StringUtils.isEmpty(cityName)||
                null==currentPage||null==pageSize){
            throw new BusinessException(ResponseCode.PARAMETER_ERROR.getCode(),
                    ResponseCode.PARAMETER_ERROR.getMessage());
        }
        //执行到这里需要自己去查询

        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.eq("city_name",cityName);
        ordersQueryWrapper.orderByDesc("create_time");

        IPage<Orders> page=new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageSize);

        //接下来分页查询数据
        IPage<Orders> ordersIPage = ordersMapper.selectPage(page, ordersQueryWrapper);
        return ordersIPage;
    }
}
